//
//  SubmitVC.m
//  DataStatistics
//
//  Created by Kang on 16/3/10.
//  Copyright © 2016年 YTYangK. All rights reserved.
//

#import "SubmitVC.h"
#import "isPhoneNumber.h"
#import "LoginViewModel.h"

@interface SubmitVC () <UITextFieldDelegate, UIAlertViewDelegate> {
    BOOL isSVCClick;
    UIToolbar* SVCtoolbar;
    UIButton* SVCbut;
    IBOutlet NSLayoutConstraint* V_nextView_H;
}
@property (strong, nonatomic) IBOutlet NSLayoutConstraint* V_password;
@property (strong, nonatomic) IBOutlet UIView *Svc_NextView;

@end

@implementation SubmitVC

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self updateUI];
 
    if (SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO_W_orH(SCREEN_HEIGHT, @"568.0")) {
        V_nextView_H.constant = 35;
        //__H_next_Y.constant -= 5;
        if (SCREEN_HEIGHT == 480) {
            _V_password.constant += 9;
            _okBtn.titleLabel.font = [UIFont systemFontOfSize:15.f];
        }
    }else if (SYSTEM_VERSION_EQUAL_TO_W_orH(SCREEN_HEIGHT,@"667.0")){
        V_nextView_H.constant = 40;
    }

    
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    _pasField.delegate = self;
    _againPasField.delegate = self;
    [_pasField addTarget:self action:@selector(SVCtextLengthMethod:) forControlEvents:UIControlEventEditingChanged];
    [_againPasField addTarget:self action:@selector(SVCtextLengthMethod:) forControlEvents:UIControlEventEditingChanged];
}
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)updateUI
{
    
    self.title = NSLocalizedString(@"SvcLead_title", nil);
    _pasText.text = NSLocalizedString(@"Lead_1", @"密码引导语");
    self.Svc_NextView.backgroundColor = RGB(26, 32, 40, 1);
    [UITextField addYTYanKTextFielStyle1:_pasField withString:NSLocalizedString(@"SvcLead_4", @"输入密码")];
    self.pasField.secureTextEntry = YES;
    [UITextField addYTYanKTextFielStyle1:_againPasField withString:NSLocalizedString(@"Lead_5", @"再输入一次")];
    self.againPasField.secureTextEntry = YES;
    [self addkeyboardRingBtn];
    self.okBtn.userInteractionEnabled = NO;
    [self.okBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [self.okBtn setTitle:NSLocalizedString(@"SvcSure", nil) forState:UIControlStateNormal];
    self.view.backgroundColor = RGB(33, 38, 44, 1.0);
    self.pasField.returnKeyType = UIReturnKeyNext;
    self.againPasField.returnKeyType= UIReturnKeyDone;
    
}


// 键盘－ 按钮
- (void)addkeyboardRingBtn
{
    SVCtoolbar = [UITextField topView];
    SVCbut =[UITextField barItem];
    [SVCbut setTitle:NSLocalizedString(@"SvcSure", nil) forState:UIControlStateNormal];
    [SVCbut addTarget:self action:@selector(okMethod:) forControlEvents:UIControlEventTouchDown];
    [SVCtoolbar addSubview:SVCbut];
    [self.againPasField setInputAccessoryView:SVCtoolbar];
}

// 限制长度
- (void)SVCtextLengthMethod:(UITextField*)textField {

    [self setSendViewBtnShow];
    if (textField == self.pasField) {
        if (textField.text.length > 12) {
            textField.text = [textField.text substringToIndex:12];
        }
    }
    else if (textField == self.againPasField) {
        if (textField.text.length > 12) {
            textField.text = [textField.text substringToIndex:12];
        }
    }
}
/// 控制 发送按钮是否显示
- (void)setSendViewBtnShow {
    if (self.pasField.text.length > 0  && self.againPasField.text.length > 0) {
        self.okBtn.userInteractionEnabled = YES;

        [self.okBtn setTitleColor:RGB(14, 118, 199, 1) forState:UIControlStateNormal];
        SVCbut.userInteractionEnabled = YES;
        [SVCbut setTitleColor:RGB(14, 118, 199, 1) forState:UIControlStateNormal];
    }
    else {
        self.okBtn.userInteractionEnabled = NO;
        [self.okBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        SVCbut.userInteractionEnabled = NO;
        [SVCbut setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    }
}

//清空输入框时调用的方法
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
    
    NSString *str = self.pasField.text;
    NSString *str1 = self.againPasField.text;
    if (str.length >0 && str1.length > 0) {
        self.okBtn.userInteractionEnabled = NO;
        [self.okBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        SVCbut.userInteractionEnabled = NO;
        [SVCbut setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    }
    
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
     [self resignRespondersForText];
}


- (void)resignRespondersForText
{
    if (isSVCClick) {
        isSVCClick = NO;
        [_pasField resignFirstResponder];
        [_againPasField resignFirstResponder];
        [self.view endEditing:YES];
    }
}

- (void)textFieldDidBeginEditing:(UITextField*)textField
{
    if (!isSVCClick) {
        isSVCClick = YES;
    }
}


- (BOOL)textFieldShouldReturn:(UITextField*)textField
{
    if (textField == self.pasField) {
        [self.againPasField becomeFirstResponder];
        
    }else{
        
        [self okMethod:_okBtn];
    }
    
    return YES;
}


#pragma mark - 注册账号请求
- (IBAction)okMethod:(UIButton*)sender
{
    
    // 注册

    NSCharacterSet* whithNewChars = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    NSString* iphonePas = [_pasField.text stringByTrimmingCharactersInSet:whithNewChars];
    NSString* againPas = [_againPasField.text stringByTrimmingCharactersInSet:whithNewChars];

    if (iphonePas.length < 6 || againPas.length < 6) {
        [MBProgressHUD yty_showErrorWithTitle:nil detailsText:NSLocalizedString(@"Lead_1", @"请输入长度为6-12位数的密码可以使用数字或字母。") toView:nil];
    }else {
        if ([isPhoneNumber illegalCharacter:iphonePas] || [isPhoneNumber illegalCharacter:againPas]) { //
            [MBProgressHUD yty_showErrorWithTitle:nil detailsText:NSLocalizedString(@"SvcLead_2", @"密码包含非法字符，请使用数字或字母输入。") toView:nil];
        }
        else {
            if ([iphonePas isEqualToString:againPas]) {
                NSDictionary* dic = @{ @"loginName" : _SVC_phoneNumber,
                                       @"zone" : _SVC_areaNam,
                                       @"nickName" :@"1",
                                       @"verifyCode" : _SVC_SMSCode,
                                       @"password" : iphonePas };
                

                [LoginViewModel requestWithURlForUserRegister:self andParam:dic success:^(id model) {
                    
                    if ([model intValue] == 1) { //成功
                        // 返回登录页
                        
                        [MBProgressHUD showSuccess:NSLocalizedString(@"SvcTip_3", @"成功")];
                        [self resignRespondersForText];

                        //修改成功延迟两秒再返回
                        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                            [self.navigationController popToRootViewControllerAnimated:YES];
                        });
                    }
                    
                } failure:^(NSString *err) {
                       [MBProgressHUD yty_showErrorWithTitle:nil detailsText:err toView:nil];
                }];
                
            }else {
                [MBProgressHUD yty_showErrorWithTitle:nil detailsText:NSLocalizedString(@"Tip_2", nil) toView:nil];
            }
        }
    }
    
   
}
@end
